<?php
class Adm extends CI_Controller
{
	var $data=array(
		'in_category'	=> '',
		'in_topic'		=> '',
		'in_setting'	=> '',
		'title'			=> '',
		'in_user'		=> '',
		'view_list'		=> '',
		'edit_cat'		=> '',
		'create_cat'	=> '',
		'topic_action'	=> '',
		'topics'		=> array(),
		'info'			=> array('id'	=> 0),
		'username'		=> '', 
		'category_path'	=> array(),
		'topic'			=>array( 'title'	 	=> '',
								 'content'		=> '',
								 'summary'		=> '',
								),
		'users'     	=>array(), //mang nay chua cac ban ghi ve user
		'topic_title'	=> '',
		'topic_content'	=> '',
		'edit_user'     =>'',
		'topic_submit_button'	=>'Create',
		'viewing_topic'		=> '',			
		'in_slide'		=> '',	
	);
	function  __construct()
	{
		parent::__construct();
		session_start();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->helper('date');
		$this->lang->load('adm');
		$this->load->model('Category','',TRUE);
		$this->load->model('Topic','',TRUE);
		$this->load->model('Config','',TRUE);
		//Kiểm tra đăng nhập ngay từ lúc khởi tạo
		if(!isset($_SESSION['username']))
		{
			if(current_url()!=site_url('adm/login')) //tránh lặp vô hạn
				redirect('adm/login');
		}
		else 
			$this->data['username'] = $_SESSION['username'];
	}
	
	//
	function login()
	{
		$this->data['notification_type']='information';
		$this->load->model('User','',TRUE);
		$username=$this->User->login();
		if($username!==false)
		{
			$_SESSION['username']=$username;
			redirect('adm');
			
		}
		else
		{
			if(isset($_POST['username']))
			{
				$this->data['notification']['type']= 'error';
				$this->data['notification']['content']='username_pass_invalid';
			}
				
			
			$this->load->helper('html');
			$this->load->view('adm/login.php',$this->data);
		
		}
	}
	function logout()
	{
		unset($_SESSION['userid']);
		redirect('adm/login');
	}
	function index()
	{
		$this->category();
	}
	function category($id = 0,$action = '',$del_id=0)
	{
		
		
		$this->data['in_category'] = 'current';
		$this->data['title'] = '';
		
		$this->data['category_path'] = $this->Category->get_category_path($id);
		$l=count($this->data['category_path']);
		if($l>0) $this->data['info'] = $this->data['category_path'][$l-1];
		else $this->data['info']['id']=0;
		
		
		$this->data['topics'] = $this->Topic->get_topics($id);
		switch($action) //kiểm tra xem hành động muốn thực hiện là j
		{
			case 'create':
				$this->data['create_cat'] = 'default-tab';
				$this->data['notification']=$this->Category->create($_POST);
			break;
			case 'update':
				
				if($l==0) break;
				$this->data['edit_cat'] = 'default-tab';
				if(isset($_POST['name'])){
					if(!isset($_POST['leftmenu'])) $_POST['leftmenu']=0;
					$this->data['notification']=$this->Category->update($_POST);
					$this->data['info'] = $this->Category->get_category_info($id);
					//redirect('adm/category/'.$id.'/');
				}
			break;
			case 'viewtopics':
				$this->data['viewing_topic'] = 'default-tab';
			break;
			case 'delete':
				if($del_id!=0){
					$this->Category->delete($del_id);
					$this->data['notification']='Delete successful!';
				}
				redirect('adm/category/'.$id);
			break;
			default:
				$this->data['view_list'] = 'default-tab';
				$this->data['sub_categories'] = $this->Category->get_sub_categories($id);
			break;
		}
		$this->load->view('adm/header',$this->data);
		$this->load->view('adm/category',$this->data);
		$this->load->view('adm/footer');
	}
	function topic($id=0,$action='update')
	{
		$this->data['in_topic'] = 'current';
		$this->data['title'] = 'Topic';
		
		$this->load->model('Topic','',TRUE);
		if($action!='create') $this->data['topic'] = $this->Topic->get_content($id);
		switch($action)
		{
			case 'create':
				
				$this->data['category_path'] = $this->Category->get_category_path($id);
				
				$this->data['info']	= $this->Category->get_category_info($id);
				
				$this->data['topic_action'] = 'create';
				$this->data['topic_submit_button'] = 'Tạo mới';
				if(isset($_POST['title']))
				{
					$config['upload_path'] = 'uploads';
					$config['allowed_types'] = 'gif|png|jpg|jpeg';
					$config['max_size'] = $this->Config->val('max_img_size');
					$this->load->library('upload', $config);
					if(!$this->upload->do_upload('image'))
					{
						break;
					}
					
					$data=$this->upload->data();
					$width=200;
					$height=intval(($width*$data['image_height'])/$data['image_width']);
					$config['image_library'] = 'gd2';
					$config['source_image'] = $data['full_path'];
					$config['maintain_ratio'] = TRUE;
					//$config['create_thumb'] = TRUE;
					$config['width'] = $width;
					$config['height'] = $height;
					$this->load->library('image_lib', $config);
                	$this->image_lib->resize();
                	$_POST['image_url'] = $data['raw_name'] . $data['file_ext'];
                	$_POST['update_date'] = now();
					$_POST['cid'] = $id;
					$this->data['notification']=$this->Topic->create($_POST);
					redirect('adm/category/'.$id);
				}
			break;
			
			case 'delete':
				$topic=$this->Topic->get_content($id);
				unlink('uploads/'.$topic['image_url']);
				$this->data['notification']=$this->Topic->delete($id);
				redirect('adm/category/'.$this->data['topic']['cid'].'/viewtopics');
			break;
			case 'update':
				$this->data['topic_submit_button'] = 'Save';
				$this->data['topic_action'] = 'update';
				
				$this->data['category_path'] = $this->Category->get_category_path($this->data['topic']['cid']);
				$this->data['info']	= $this->Category->get_category_info($this->data['topic']['cid']);
				
				if(isset($_POST['title']))
				{
					$_POST['update_date'] = now();
					$_POST['id'] = $id;
					$_POST['cid'] = $this->data['topic']['cid'];
					$this->data['notification']=$this->Topic->update($_POST);
					//$this->data['topic'] = $this->Topic->get_content($id);
				}
			break;
		}
		$this->load->view('adm/header',$this->data);
		$this->load->view('adm/topic',$this->data);
		$this->load->view('adm/footer');
	}
	function user($id = 0,$action = '') 
	{
		$this->data['in_user'] = 'current';
		$this->data['title']   = 'User';
		
		$this->load->model('User','',TRUE);//
		//load user vào biến $this->data['users'] để tí load ở views
		$this->data['users'] = $this->User->get_users();
		//tam thoi chua can xu ly j, viet xem no co hien len giao dien trang theo yeu cau ko
		$this->load->view('adm/header',$this->data);
		$this->load->view('adm/user',$this->data);
		$this->load->view('adm/footer',$this->data);
		switch ($action)
		{
			case 'edit':
				$this->data['edit_user'] = 'default-tab';
				if(isset($_POST['name']))
				{
					$this->data['notification']=$this->Category->update($_POST);
					$this->data['info'] = $this->Category->get_user($id);
				}	
		}
	}
	function setting($action='')
	{
		$this->data['in_setting'] = 'current';
		
		$this->data['title'] = $this->lang->line('CONFIGS');
		$this->load->model('Config','',TRUE);
		$configs=$this->Config->get_all();
		
		if($action=='update')
		{
			$this->Config->update_all($_POST);
			$configs=$this->Config->get_all();
			
			$this->data['notification'] = array('type'	=>'success',
												'content'	=> 'CONFIGS_UPDATE_SUCCESS'
												);
		}
		$this->data['configs'] = $configs;
		$this->load->view('adm/header',$this->data);
		$this->load->view('adm/setting');
		$this->load->view('adm/footer');
	}
	function slide($action='',$id=0)
	{
		$this->load->model('Slide','',TRUE);
		
		$this->data['in_slide']	= 'current';
		$images=$this->Slide->get_all();
		switch($action)
		{
			case 'new_img':
				$config['upload_path'] = $this->Config->val('slide_path');
				$config['allowed_types'] = 'gif|png|jpg|jpeg';
				$config['max_size'] = $this->Config->val('max_img_size');
				$this->load->library('upload', $config);
				if(!$this->upload->do_upload('image'))
				{
					break;
				}
					
				$data=$this->upload->data();
				$width=960;
				$height=intval(($width*$data['image_height'])/$data['image_width']);
				$config['image_library'] = 'gd2';
				$config['source_image'] = $data['full_path'];
				$config['maintain_ratio'] = TRUE;
				//$config['create_thumb'] = TRUE;
				$config['width'] = $width;
				$config['height'] = $height;
				$this->load->library('image_lib', $config);
				$this->image_lib->resize();
				$_POST['url'] = $config['upload_path'] .'/'. $data['raw_name'] . $data['file_ext'];
				$this->Slide->new_img($_POST);
				redirect('adm/slide');
				break;
			case 'update':
				$this->Slide->update($_POST);
				redirect('adm/slide');
				break;
			case 'delete':
				unlink($this->Slide->delete($id));
				redirect('adm/slide');
				break;
		}
		$this->data['images']=$images;
		$this->load->view('adm/header',$this->data);
		$this->load->view('adm/slide');
		$this->load->view('adm/footer');
	}
}
?>